package com.nogroup.springbootproject.controller;

import com.nogroup.springbootproject.dto.ExamDto;
import com.nogroup.springbootproject.entity.Exam;
import com.nogroup.springbootproject.service.ExamService;
import com.nogroup.springbootproject.utils.PageUtils;
import com.nogroup.springbootproject.utils.result.DataResult;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.List;

/**
 * (Exam)表控制层
 *
 * @author makejava
 * @since 2024-04-07 18:45:32
 */
@RestController
@RequestMapping("exam")
public class ExamController {
    /**
     * 服务对象
     */
    @Resource
    private ExamService examService;

    /**
     * 分页查询
     *
     * @param exam 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Exam>> queryByPage(Exam exam, PageRequest pageRequest) {
        return ResponseEntity.ok(this.examService.queryByPage(exam, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Exam> queryById(@PathVariable("id") String id) {
        return ResponseEntity.ok(this.examService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param exam 实体
     * @return 新增结果
     */
    @RequestMapping("insertExam")
    @PostMapping
    public DataResult insertExam(Exam exam) {
        System.out.println("exam:" + exam.toString());
        return DataResult.successByData(examService.insert(exam));
    }

    /**
     * 编辑数据
     *
     * @param exam 实体
     * @return 编辑结果
     */
    @RequestMapping("edit")
    @PutMapping
    public DataResult edit(Exam exam) {
        return DataResult.successByData(this.examService.update(exam));
    }

    /**
     * 删除数据
     *
     * @param exam 主键
     * @return 删除是否成功
     */
    @RequestMapping("deleteById")
    @DeleteMapping
    public DataResult deleteById(Exam exam) {

        return DataResult.successByData(this.examService.deleteById(exam));
    }

    @RequestMapping("getAllExams")
    public DataResult getAllExams(){
        List<Exam> exams = examService.getAllExams();
        return DataResult.successByDatas(exams);
    }

    @RequestMapping("getExamsByFuzzySearch")
    public DataResult getExamsByFuzzySearch(ExamDto examDto){
        long count = examService.countPage(examDto);
        int index = PageUtils.getStartPage(examDto.getPage(),examDto.getLimit());
        System.out.println("examDto:" + examDto.getExamName());
        examDto.setPage(index < count ? index : 0);
        List<Exam> exams = examService.getExamsByFuzzySearch(examDto);
        exams.forEach(exam -> {
            System.out.println("exam:" + exam.getExamCourseName());
        });
        System.out.println("examDto:" + examDto.toString());
        return DataResult.successByTotalData(exams,count);
    }
}

